import { Controller, Get, Logger, Req } from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import { IsNumber, IsString } from "class-validator";
import { Request } from "express";

import { AppService } from "./app.service";
import { SkipJwtAuth } from "./core/auth/decorator/skip-auth.decorator";
import { PrismaService } from "./core/db/prisma.service";
import { RequestUrlUtil } from "./core/utils/request-url.utils";

class User {
    @IsString()
    private username?: string;

    @IsString()
    private password: string = "aaaaaaa";

    @IsNumber()
    private age?: number;
    constructor(username: string, age: number) {
        this.username = username;
        this.age = age;
    }
}

@ApiTags("app")
@Controller("app")
@SkipJwtAuth()
export class AppController {
    private readonly logger = new Logger(AppController.name);
    constructor(
        private readonly appService: AppService,
        private readonly prismaService: PrismaService
        // private readonly logger1: WinstonLoggerService
    ) {
        // this.logger1.setContext(AppController.name);
    }

    @Get("debug-sentry")
    getError() {
        // Send a log before throwing the error
        // Sentry.logger.info("User triggered test error", {
        //     action: "test_error_endpoint"
        // });
        // JSON.parse("invalid json}");
        throw new Error("My first Sentry error!");
    }

    @Get("/log")
    logTest(@Req() req: Request): string {
        // JSON.parse("invalid json}");
        console.log("req.id", req.id);
        this.logger.log("Hello endpoint called");
        this.logger.debug("This is debug info");
        this.logger.verbose("verbose"); // ✅ 输出（你启用了）
        this.logger.debug("debug"); // ✅ 输出（你启用了）
        this.logger.log("log"); // ✅ 输出（你启用了）
        this.logger.warn("warn"); // ✅ 输出（强制启用！）
        this.logger.error("error"); // ✅ 输出（强制启用！）
        this.logger.fatal("fatal"); // ✅ 输出（强制启用！）

        return "Hello World!";
    }

    @Get()
    getHello(@Req() req: Request): string {
        console.log(req.protocol, req.hostname, req.ip, req.originalUrl, req.method, req.path);
        console.log("RequestUrlUtil.getFullUrl(req);", RequestUrlUtil.getFullUrl(req));
        // throw new NotFoundException();
        // throw new BadRequestException();
        // throw new HttpException("Forbidden", HttpStatus.OK);
        // console.log('BusinessException("FAILED").name', new BusinessException("FAILED").name);
        // throw new BusinessException();
        // JSON.parse("invalid json}");
        return this.appService.getHello();
    }
}
